package com.zcx.common;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.concurrent.*;

public class ThreadDemo {
    public static void main(String[] args) {
        ThreadDemo threadDemo = new ThreadDemo();
        //无返回值的线程池
        threadDemo.voidThread();
        //有返回值的线程池
        threadDemo.captureDataThread();
    }


    private void voidThread() {
        TaskExecutorConfiguration taskExecutorConfiguration = new TaskExecutorConfiguration();
        Executor executor = taskExecutorConfiguration.schedulingTaskExecutor();
        for (int i = 0; i < 10; i++) {
            executor.execute(new Runnable() {
                @Override
                public void run() {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
                    System.out.println("thread"+Thread.currentThread().getName()+",time:"+sdf.format(new Date()));
                }
            });
        }
    }

    private void captureDataThread() {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        ArrayList<Future<StudentBean>> futures = new ArrayList<>();
        final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        for (int i = 0; i < 50; i++) {
            final int finalIi = i;
            Future<StudentBean> ss = executorService.submit(new Callable<StudentBean>() {
                @Override
                public StudentBean call() throws Exception {
                    System.out.println("thread:" + Thread.currentThread().getName() + ",time:" + sdf.format(new Date()));
                    return init(finalIi);
                }
            });
            futures.add(ss);
        }
        executorService.shutdown();
            futures.stream().forEach(e->{
                try{
                    System.out.println(e.get());
                }catch (InterruptedException ex){
                    ex.printStackTrace();
                }catch (ExecutionException ex){
                    ex.printStackTrace();
                }
            });
    }

    private static StudentBean init(int i) {
        int ll = i;
        return new StudentBean("刘七"+i,"老王"+i,26,"幼稚园"+i,32,"英语"+i);
    }


}
